<script lang="ts" setup>
import {
  Upload,
  Avatar
} from '@element-plus/icons-vue'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'

import { userAvatarUpdateService } from '@/api/user'
//个人信息
import useUserInfoStore from '@/stores/userInfo'
const userInfo = useUserInfoStore()
//令牌
import { useTokenStore } from '@/stores/token'
const tokenStore = useTokenStore()

import type { UploadProps } from 'element-plus'

const imageUrl = ref(userInfo.info.photo)

const handleAvatarSuccess: UploadProps['onSuccess'] = async (
  response,
  uploadFile
) => {
  // imageUrl.value = URL.createObjectURL(uploadFile.raw!)
  if (response.code === 1) {
    const maxSize = 2 * 1024 * 1024; // 2MB
    if (uploadFile.size > maxSize) {
      ElMessage.error('上传图片大小不能超过 2MB!');
    } else
      ElMessage.error('图片上传失败')
  } else {
    console.log(response.data)
    imageUrl.value = "/api/img/" + response.data
    console.log(imageUrl.value)
  }
}


const uploadAvatar = async () => {
  let result = await userAvatarUpdateService(imageUrl.value);
  ElMessage.success(result.msg ? result.msg : '修改成功')
  userInfo.info.photo = imageUrl.value;
}

//点击按钮 => 点击图片均能选择图片
import { onMounted } from 'vue';
import { fi } from 'element-plus/es/locale'
const uploadRef = ref(null);
onMounted(() => {
  // console.log(uploadRef.value);
});
function selectImage() {
  // 使用uploadRef.value来访问组件实例
  if (uploadRef.value) {
    uploadRef.value.$el.querySelector('input').click();
  }
}
</script>

<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>更换头像</span>
      </div>
    </template>
    <el-row>
      <el-col :span="10">
        <el-upload ref="uploadRef" class="avatar-uploader" name="file" action="/api/upload" :show-file-list="false"
          :headers="{ 'Authorization': tokenStore.token }"
          :on-success="handleAvatarSuccess">
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <img v-else :src="avatar" width="278" />
        </el-upload>
        <br />
        <el-button type="primary" :icon="Plus" size="large" @click="selectImage">
          选择图片
        </el-button>
        <el-button type="success" :icon="Upload" size="large" @click="uploadAvatar">
          上传头像
        </el-button>
      </el-col>
    </el-row>
  </el-card>
</template>

<style lang="scss" scoped>
.avatar-uploader {
  :deep() {
    .avatar {
      width: 278px;
      height: 278px;
      display: block;
    }

    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }

    .el-upload:hover {
      border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 278px;
      height: 278px;
      text-align: center;
    }
  }
}
</style>